//package org.dromara.oce.utils;
//
//import cn.hutool.json.JSONUtil;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import org.apache.http.HttpHost;
//import org.dromara.oce.domain.OceAdvertiserEverydayCost;
//import org.elasticsearch.action.bulk.BulkRequest;
//import org.elasticsearch.action.bulk.BulkResponse;
//import org.elasticsearch.action.delete.DeleteRequest;
//import org.elasticsearch.action.get.GetRequest;
//import org.elasticsearch.action.get.GetResponse;
//import org.elasticsearch.action.index.IndexRequest;
//import org.elasticsearch.action.index.IndexResponse;
//import org.elasticsearch.action.search.SearchRequest;
//import org.elasticsearch.action.search.SearchResponse;
//import org.elasticsearch.action.update.UpdateRequest;
//import org.elasticsearch.action.update.UpdateResponse;
//import org.elasticsearch.client.RequestOptions;
//import org.elasticsearch.client.RestClient;
//import org.elasticsearch.client.RestHighLevelClient;
//import org.elasticsearch.client.indices.CreateIndexRequest;
//import org.elasticsearch.client.indices.CreateIndexResponse;
//import org.elasticsearch.common.text.Text;
//import org.elasticsearch.common.xcontent.XContentType;
//import org.elasticsearch.index.query.BoolQueryBuilder;
//import org.elasticsearch.index.query.QueryBuilder;
//import org.elasticsearch.index.query.QueryBuilders;
//import org.elasticsearch.index.query.TermQueryBuilder;
//import org.elasticsearch.search.SearchHits;
//import org.elasticsearch.search.aggregations.AggregationBuilder;
//import org.elasticsearch.search.aggregations.AggregationBuilders;
//import org.elasticsearch.search.builder.SearchSourceBuilder;
//import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
//import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
//import org.elasticsearch.search.sort.SortOrder;
//
//import java.io.IOException;
//import java.util.Arrays;
//import java.util.Map;
//
//public class EsDocTest {
//
//    public static void main(String[] args) {
//
//        RestHighLevelClient client = new RestHighLevelClient(
//            RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))
//        );
//        try {
//            IndexRequest indexRequest = new IndexRequest();
//            indexRequest.index("hhl_index").id("1");
//
//            OceAdvertiserEverydayCost oce = new OceAdvertiserEverydayCost();
//            oce.setAdvertiserCostId(1L);
//            oce.setThatdayDate("2025-05-01");
//            oce.setThirdAdvertiserId("1111");
//            oce.setThirdAdvertiserName("aaaaaaaaa");
//            oce.setThirdAdvertiserAppCode("my_tf");
//
//            OceAdvertiserEverydayCost oce2 = new OceAdvertiserEverydayCost();
//            oce2.setAdvertiserCostId(2L);
//            oce2.setThatdayDate("2025-05-01");
//            oce2.setThirdAdvertiserId("2222");
//            oce2.setThirdAdvertiserName("bbbbbbbb");
//            oce2.setThirdAdvertiserAppCode("my_tf");
//
//            OceAdvertiserEverydayCost oce3 = new OceAdvertiserEverydayCost();
//            oce3.setAdvertiserCostId(3L);
//            oce3.setThatdayDate("2025-05-01");
//            oce3.setThirdAdvertiserId("3333");
//            oce3.setThirdAdvertiserName("ccccccc");
//            oce3.setThirdAdvertiserAppCode("my_tf");
//
////            ObjectMapper objectMapper = new ObjectMapper();
////            indexRequest.source(JSONUtil.toJsonStr(oce), XContentType.JSON);
////            IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
////            System.out.println(response.getResult());
//
//
//            //局部更新
////            UpdateRequest updateRequest = new UpdateRequest();
////            updateRequest.index("hhl_index").id("1");
////            updateRequest.doc("{\"thirdAdvertiserName\":\"cccccc\"}", XContentType.JSON);
////            UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
////            System.out.println(updateResponse.getResult());
//
//            //查询数据
////            GetRequest getRequest = new GetRequest();
////            getRequest.index("hhl_index").id("1");
////            GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
////            String sourceAsString = getResponse.getSourceAsString();
////            System.out.println(sourceAsString);
//
//
//            //批量新增
////            BulkRequest bulkRequest = new BulkRequest();
////            bulkRequest.add(new IndexRequest().index("hhl_index").id("1").source(JSONUtil.toJsonStr(oce), XContentType.JSON));
////            bulkRequest.add(new IndexRequest().index("hhl_index").id("2").source(JSONUtil.toJsonStr(oce2), XContentType.JSON));
////            bulkRequest.add(new IndexRequest().index("hhl_index").id("3").source(JSONUtil.toJsonStr(oce3), XContentType.JSON));
////            BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
////            System.out.println(bulkResponse.getTook());
////            System.out.println(bulkResponse.getItems());
//
//            //批量删除
////            BulkRequest bulkRequest = new BulkRequest();
////            bulkRequest.add(new DeleteRequest().index("hhl_index").id("1"));
////            bulkRequest.add(new DeleteRequest().index("hhl_index").id("2"));
////            bulkRequest.add(new DeleteRequest().index("hhl_index").id("3"));
////            BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
////            System.out.println(bulkResponse.getTook());
////            System.out.println(bulkResponse.getItems());
//
//
//            //全量查询
////            SearchRequest searchRequest = new SearchRequest();
////            searchRequest.indices("hhl_index");
////            searchRequest.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
////            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
////            SearchHits hits = searchResponse.getHits();
////            System.out.println("总数据："+hits.getTotalHits());
////            for (var hit : hits) {
////                System.out.println(hit.getSourceAsString());
////            }
//
//            //条件查询
////            SearchRequest searchRequest = new SearchRequest();
////            searchRequest.indices("hhl_index");
////            searchRequest.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("thirdAdvertiserName", "dddadf")));
////            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
////            SearchHits hits = searchResponse.getHits();
////            System.out.println("总数据："+hits.getTotalHits());
////            for (var hit : hits) {
////                System.out.println(hit.getSourceAsString());
////            }
//
//            //分页查询
////            SearchRequest searchRequest = new SearchRequest();
////            searchRequest.indices("hhl_index");
////
////            SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.termQuery("thirdAdvertiserName", "dddadf")).from(0).size(2);
////            searchRequest.source(builder);
////            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
////            SearchHits hits = searchResponse.getHits();
////            System.out.println("总数据："+hits.getTotalHits());
////            for (var hit : hits) {
////                System.out.println(hit.getSourceAsString());
////            }
//
//            //排序查询
////            SearchRequest searchRequest = new SearchRequest();
////            searchRequest.indices("hhl_index");
////
////            SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()).from(0).size(10).sort("advertiserCostId", SortOrder.DESC);
////            searchRequest.source(builder);
////            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
////            SearchHits hits = searchResponse.getHits();
////            System.out.println("总数据："+hits.getTotalHits());
////            for (var hit : hits) {
////                System.out.println(hit.getSourceAsString());
////            }
//
//            //指定返回字段
////            SearchRequest searchRequest = new SearchRequest();
////            searchRequest.indices("hhl_index");
////            SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()).from(0).size(10).sort("advertiserCostId", SortOrder.DESC)
////                .fetchSource(new String[]{"advertiserCostId","thirdAdvertiserName"},new String[]{});
////            searchRequest.source(builder);
////            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
////            SearchHits hits = searchResponse.getHits();
////            System.out.println("总数据："+hits.getTotalHits());
////            for (var hit : hits) {
////                System.out.println(hit.getSourceAsString());
////            }
//
////            SearchRequest searchRequest = new SearchRequest();
////            searchRequest.indices("hhl_index");
////            // 组合查询示例1: must + should组合
////            SearchSourceBuilder builder = new SearchSourceBuilder();
////            BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
////            // must条件：必须满足
////            boolQuery.must(QueryBuilders.termQuery("thirdAdvertiserAppCode", "my_tf"));
////            // should条件：满足其中一个或多个
////            boolQuery.should(QueryBuilders.matchQuery("thirdAdvertiserName", "aaaaaaaaa"));
////            boolQuery.should(QueryBuilders.matchQuery("thirdAdvertiserName", "bbbb"));
////
////            // 设置最小should匹配数
////            boolQuery.minimumShouldMatch(1);
////            builder.query(boolQuery).from(0).size(10);
////            searchRequest.source(builder);
////
////            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
////            SearchHits hits = searchResponse.getHits();
////            System.out.println("总数据：" + hits.getTotalHits());
////            for (var hit : hits) {
////                System.out.println(hit.getSourceAsString());
////            }
//
//            //范围查询
////            SearchRequest searchRequest = new SearchRequest();
////            searchRequest.indices("hhl_index");
////            // 组合查询示例3: 范围查询与文本查询组合
////            SearchSourceBuilder builder = new SearchSourceBuilder();
////            BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
////
////            // 组合范围查询和文本匹配
////            boolQuery.must(QueryBuilders.rangeQuery("advertiserCostId").gt(1));
//////            boolQuery.must(QueryBuilders.wildcardQuery("thirdAdvertiserName", "*aaa*"));
////
////            builder.query(boolQuery)
////                .from(0)
////                .size(10)
////                .fetchSource(new String[]{"advertiserCostId", "thirdAdvertiserName"}, new String[]{});
////
////            searchRequest.source(builder);
////            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
////            for (var hit : searchResponse.getHits()) {
////                System.out.println(hit.getSourceAsString());
////            }
//
//            //高亮查询
////            SearchRequest searchRequest = new SearchRequest();
////            searchRequest.indices("hhl_index");
////            SearchSourceBuilder builder = new SearchSourceBuilder();
////            // 设置查询条件
////            TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("thirdAdvertiserName", "aaaaaaaaa");
////            builder.query(termQueryBuilder);
////
////            // 设置高亮
////            HighlightBuilder highlightBuilder = new HighlightBuilder();
////            // 设置高亮字段
////            highlightBuilder.field("thirdAdvertiserName");
////            // 设置高亮前缀和后缀标签
////            highlightBuilder.preTags("<em style='color:red'>");
////            highlightBuilder.postTags("</em>");
////            // 设置高亮片段大小
////            highlightBuilder.fragmentSize(150);
////            // 设置返回的高亮片段数量
////            highlightBuilder.numOfFragments(3);
////
////            builder.highlighter(highlightBuilder);
////            builder.from(0).size(10);
////
////            searchRequest.source(builder);
////            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
////            SearchHits hits = searchResponse.getHits();
////
////            System.out.println("总数据：" + hits.getTotalHits());
////            for (var hit : hits) {
////                System.out.println("原始数据: " + hit.getSourceAsString());
////                System.out.println(hit.getHighlightFields());
////
////                // 获取高亮结果
////                Map<String, HighlightField·> highlightFields = hit.getHighlightFields();
////                if (highlightFields != null && !highlightFields.isEmpty()) {
////                    HighlightField highlightField = highlightFields.get("thirdAdvertiserName");
////                    if (highlightField != null) {
////                        Text[] fragments = highlightField.fragments();
////                        if (fragments != null && fragments.length > 0) {
////                            System.out.println("高亮结果: " + Arrays.toString(fragments));
////                        }
////                    }
////                }
////            }
//
//
//            // 聚合查询
//            SearchRequest searchRequest = new SearchRequest();
//            searchRequest.indices("hhl_index");
//            SearchSourceBuilder builder = new SearchSourceBuilder();
//
////            AggregationBuilder aggregationBuilder = AggregationBuilders.max("maxAdvertiserCostId").field("advertiserCostId");
//            AggregationBuilder aggregationBuilder = AggregationBuilders.avg("avgAdvertiserCostId").field("advertiserCostId");
//            builder.aggregation(aggregationBuilder);
//            builder.size(0);
//
//            searchRequest.source(builder);
//            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
//            SearchHits hits = searchResponse.getHits();
//
//            System.out.println(JSONUtil.toJsonStr(searchResponse.getAggregations()));
//            System.out.println("总数据：" + hits.getTotalHits());
//            for (var hit : hits) {
//                System.out.println("原始数据: " + hit.getSourceAsString());
//            }
//
//
//            System.out.println(1);
//        } catch (Exception e) {
//            e.printStackTrace();
//            throw new RuntimeException(e);
//        } finally {
//            try {
//                client.close();
//            } catch (IOException e) {
//                throw new RuntimeException(e);
//            }
//        }
//
//    }
//}
